Skip to content

[BUG] Fix deep_equals failing on string-dtype numpy arrays (numpy < 2.0)#519

Open
shourya1419-netizen wants to merge 9 commits intosktime:mainfrom
shourya1419-netizen:fix_numpy_deep_equals
Open

[BUG] Fix deep_equals failing on string-dtype numpy arrays (numpy < 2.0)#519
shourya1419-netizen wants to merge 9 commits intosktime:mainfrom
shourya1419-netizen:fix_numpy_deep_equals

Conversation

@shourya1419-netizen
Copy link
Copy Markdown

Fixes #517

What was the bug?

deep_equals crashed with a TypeError when comparing numpy arrays
with string dtype on numpy < 2.0.

The check if x.dtype == "str" in _numpy_equals_plugin never matched
in numpy < 2.0, because string arrays have dtype like U3 or <U1
(not "str"). This caused string arrays to fall through to the else
branch which calls np.array_equal(x, y, equal_nan=True) — and
equal_nan=True internally calls np.isnan(), which does not support
string types.

Fix

Added np.issubdtype(x.dtype, np.character) to the dtype check, which
correctly catches all string dtypes across numpy versions (both < 2.0
and >= 2.0).

Testing

Added a new test file tests/test_deep_equals.py covering:

  • equal string arrays return True
  • unequal string arrays return False

All tests pass.

Copy link
Copy Markdown
Contributor

@fkiraly fkiraly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The test would not catch it though because afaik we are testing only with numpy 2. I also opened an issue, hence, to add test coverage here: #532

@fkiraly
Copy link
Copy Markdown
Contributor

fkiraly commented Apr 18, 2026

Abandoned PR. Merging without added test since the fix seems reasonable.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 18, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.54%. Comparing base (306958d) to head (d315731).
⚠️ Report is 193 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #519      +/-   ##
==========================================
- Coverage   85.07%   83.54%   -1.54%     
==========================================
  Files          45       52       +7     
  Lines        3015     3913     +898     
==========================================
+ Hits         2565     3269     +704     
- Misses        450      644     +194     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] deep_equals fails on string-dtype numpy arrays (numpy < 2.0)

2 participants